﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace syc.Fc
{
    public partial class 入库数据检校 : System.Web.UI.Page
    {
        protected string msg = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                if ((object)Session["OperatorDID"] != null)
                {
                    int did = (int)Session["OperatorDID"];
                    if (did != 0)
                    {
                        if (did == 1)
                        {
                            this.ShengChan.Text = "1";
                            this.ShengChan.Enabled = false;
                        }
                        else if (did == 2)
                        {
                            this.ShengChan.Text = "2";
                            this.ShengChan.Enabled = false;
                        }
                        else if (did == 5)
                        {
                            this.ShengChan.Enabled = true;
                            this.ShengChan.Items.Clear();
                            ListItem li = new ListItem();
                            li.Text = "五分厂1号线";
                            li.Value = "501";
                            this.ShengChan.Items.Add(li);
                            ListItem li2 = new ListItem();
                            li2.Text = "五分厂2号线";
                            li2.Value = "502";
                            this.ShengChan.Items.Add(li2);
                        }
                    }
                }
            }
            else
            {
                this.SqlDataSource1.SelectCommand = this.SqlText.Text;
            }
        }

        protected void GridView1_DataBound(object sender, EventArgs e)
        {
            this.TextBox_Pager.Text = (this.GridView1.PageIndex + 1).ToString();
            this.Label_Pages.Text = " 共" + this.GridView1.PageCount.ToString() + "页 ";

            this.Button_Pager_First.CssClass = "pageturn-first-b";
            this.Button_Pager_First.Enabled = true;
            this.Button_Pager_Prev.CssClass = "pageturn-prev-b";
            this.Button_Pager_Prev.Enabled = true;
            this.Button_Pager_Next.CssClass = "pageturn-next-b";
            this.Button_Pager_Next.Enabled = true;
            this.Button_Pager_Last.CssClass = "pageturn-last-b";
            this.Button_Pager_Last.Enabled = true;
            if (this.GridView1.PageIndex == 0)
            {
                this.Button_Pager_Prev.CssClass = "pageturn-prev-g";
                this.Button_Pager_Prev.Enabled = false;
            }
            if (this.GridView1.PageIndex < 2)
            {
                this.Button_Pager_First.CssClass = "pageturn-first-g";
                this.Button_Pager_First.Enabled = false;
            }
            if (this.GridView1.PageIndex == this.GridView1.PageCount - 1 || this.GridView1.PageCount == 0)
            {
                this.Button_Pager_Next.CssClass = "pageturn-next-g";
                this.Button_Pager_Next.Enabled = false;

            }
            if (this.GridView1.PageIndex > this.GridView1.PageCount - 3)
            {
                this.Button_Pager_Last.CssClass = "pageturn-last-g";
                this.Button_Pager_Last.Enabled = false;
            }

        }
        protected void GridView1_PageIndexChanged(object sender, EventArgs e)
        {
            this.GridView1_DataBound(null, null);
        }
        protected void Pager_Click(object sender, EventArgs e)
        {
            if (sender.GetType().ToString() == "System.Web.UI.WebControls.Button")
            {
                switch (((Button)sender).CommandName)
                {
                    case "--":
                        this.GridView1.PageIndex = 0;
                        break;
                    case "-":
                        if (this.GridView1.PageIndex > 0) this.GridView1.PageIndex--;
                        break;
                    case "+":
                        if (this.GridView1.PageIndex < this.GridView1.PageCount - 1) this.GridView1.PageIndex++;
                        break;
                    case "++":
                        this.GridView1.PageIndex = this.GridView1.PageCount - 1;
                        break;
                }
            }
            else
            {

                int p;
                if (int.TryParse(TextBox_Pager.Text, out p))
                {
                    if (p >= 1 && p <= this.GridView1.PageCount)
                    {
                        this.GridView1.PageIndex = p - 1;
                    }
                }
                TextBox_Pager.Text = (this.GridView1.PageIndex + 1).ToString();
            }

        }
        protected void SqlDataSource1_Selected(object sender, SqlDataSourceStatusEventArgs e)
        {
            Label_Total.Text = "共查询到 " + e.AffectedRows + " 项记录";
        }
        private string GetState()
        {
            DateTime d1, d2;
            if (!DateTime.TryParse(this.Date1.Text, out d1))
            {
                return null;
            }

            if (!DateTime.TryParse(this.Date2.Text, out d2))
            {
                return null;
            }
            string state = "";
            int scx = 1;
            int fenchang = 1;
            if (this.ShengChan.Text == "1")
            {
                fenchang = 1;
            }
            else if (this.ShengChan.Text == "2")
            {
                fenchang = 2;
            }
            else if (this.ShengChan.Text == "501")
            {
                fenchang = 5;
            }
            else if (this.ShengChan.Text == "501")
            {
                scx = 2;
                fenchang = 5;
            }

            state = " where 生产线=" + (fenchang*100+scx)
                + " and 产品类别ID=" + this.ChanPin.Text
                + " and 日期 between '" + d1.ToShortDateString() + "' and '" + d2.ToShortDateString() + "'";

            return state;
        }
        private string GetState2()
        {
            DateTime d1, d2;
            DateTime.TryParse(this.Date1.Text, out d1);
            DateTime.TryParse(this.Date2.Text, out d2);

            string state = "";
            int scx = 1;
            int fenchang = 1;
            if (this.ShengChan.Text == "1")
            {
                fenchang = 1;
            }
            else if (this.ShengChan.Text == "2")
            {
                fenchang = 2;
            }
            else if (this.ShengChan.Text == "501")
            {
                fenchang = 5;
            }
            else if (this.ShengChan.Text == "501")
            {
                scx = 2;
                fenchang = 5;
            }

            state = " where 生产线=" + scx +" and 分厂ID="+fenchang
                + " and 产品类别ID=" + this.ChanPin.Text
                + " and 日期 between '" + d1.ToShortDateString() + "' and '" + d2.ToShortDateString() + "'";

            return state;
        }
        protected void Button_Query_Click(object sender, EventArgs e)
        {
            string state = this.GetState();
            if (state == null)
            {
                this.msg = "请输入日期范围";
                return;
            }
            this.SqlText.Text =

@"SELECT
入库日期 as 日期
,'库存' as 数据
,生产线
,COUNT(id) as 捆数
,sum(支数) as 支数
,sum(重量) as 重量
,sum(长度) as 长度
FROM [产品库存] "
+ state.Replace("日期", "入库日期") +
@" group by 入库日期,生产线
union all
SELECT 
日期
,'入库单' as 数据
,生产线
,sum(总件数) as 捆数
,sum(总支数) as 支数
,sum(总重量) as 重量
,sum(总长度) as 长度
FROM 产品入库单"
+ state +
@" group by 日期,生产线

union all

SELECT 
日期
,'日进度' as 数据
,分厂ID*100+生产线 as 生产线
,sum(入库捆数) as 捆数
,sum(入库支数) as 支数
,sum(入库重量) as 重量
,sum(入库长度) as 长度
FROM [生产进度]"
+ GetState2()+
@" group by 日期,分厂ID,生产线,产品类别ID order by 日期";

            this.SqlDataSource1.SelectCommand = "";
            this.SqlDataSource1.SelectCommand = this.SqlText.Text;
        }
    }
}
